package com.mrx.compose.weather.api.qweather

import com.google.gson.reflect.TypeToken
import com.mrx.compose.weather.api.ApiHelper
import com.mrx.compose.weather.api.annotations.Api
import retrofit2.http.GET
import retrofit2.http.Path

@Api("http://guolin.tech")
interface CityAPI {

    @GET("/api/china/{province}/{city}")
    suspend fun findDistrict(
        @Path("province") province: Int,
        @Path("city") city: Int
    ): List<CityAPIResponse>

    data class CityAPIResponse(
        var id: Int? = null,
        var name: String? = null,
        var weather_id: String? = null
    )

    data class CityInfo(
        var id: Int? = null,
        var list: List<CityInfo> = listOf(),
        var name: String? = null
    )

    data class CityInfoHolder(
        var province: CityInfo? = null,
        var city: CityInfo? = null,
        var district: CityInfo? = null
    )

    companion object {
        
        val cityInfo: List<CityInfo> = ApiHelper.gson.fromJson<List<CityInfo>>(
            CityAPI::class.java.getResourceAsStream("/weather/cityInfo.json")!!
                .readAllBytes().decodeToString(), object : TypeToken<List<CityInfo>>() {}.type
        )

        fun List<CityAPIResponse>.findDistrict(district: Int) = this.find { it.id == district }!!
    }
}